package com.jzt.qf.task;

import com.jzt.qf.beans.HttpResult;
import com.jzt.qf.beans.qfBean.ZxyBasicinfoJzt;
import com.jzt.qf.dao.BasicinfoJztDao;
import com.jzt.qf.service.BtypeService;
import com.jzt.qf.service.PtypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class UploadBasicInfoTask {
    @Resource
    private BasicinfoJztDao basicinfoJztDao;
    @Resource
    private PtypeService ptypeService;

    //TODO 上传商品信息
    @Scheduled(fixedDelay = 80000)  //间隔60秒1000000
    public void uploadPtype() {
        log.info("上传商品信息开始...");
        List<ZxyBasicinfoJzt> zxyBasicinfoList = basicinfoJztDao.getNeedUpLoadBasicInfoByTypes("P");
        for (ZxyBasicinfoJzt zxyBasicinfo : zxyBasicinfoList) {
            HttpResult httpResult = ptypeService.uploadPtype(zxyBasicinfo.getRec() + "");
            try {
                if (httpResult.isFlag()) {
                    //如果上传成功,删除中间库数据
                    basicinfoJztDao.delNeedUpLoadBasicInfoByTypes(zxyBasicinfo.getRec() + "", "P");
                }
            } catch (Exception e) {
                log.info("上传商品信息出错" + e.getMessage());
            }
        }

    }

    @Resource
    private BtypeService btypeService;

    //TODO 供应商和客户
    @Scheduled(fixedDelay = 90000)  //间隔60秒1000000
    public void uploadBtype() {
        List<ZxyBasicinfoJzt> zxyBasicinfoList = basicinfoJztDao.getNeedUpLoadBasicInfoByTypes("B");
        for (ZxyBasicinfoJzt zxyBasicinfo : zxyBasicinfoList) {
            HttpResult httpResult = btypeService.uploadBtype(zxyBasicinfo.getRec() + "");
            try {
                if (httpResult.isFlag()) {
                    //如果上传成功,删除中间库数据
                    basicinfoJztDao.delNeedUpLoadBasicInfoByTypes(zxyBasicinfo.getRec() + "", "B");
                }
            } catch (Exception e) {
                log.info("上传往来单位出错" + e.getMessage());
            }
        }
    }
}
